package com.acg.auth.controller;


import com.acg.auth.pojo.SysMenu;
import com.acg.auth.service.SysMenuService;
import com.acg.auth.vo.AssignMenuVo;
import com.acg.exception.ACGException;
import com.acg.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 菜单表 前端控制器
 * </p>
 *
 * @author acg
 * @since 2025-08-08
 */

@CrossOrigin("*")
@RestController
@RequestMapping("/acg/menu")
public class SysMenuController {

    @Autowired
    private SysMenuService sysMenuService;


    /**
     * 获取所有菜单（返回树状数据）
     */
    @GetMapping("getNodeTree")
    public Result getAllMenu() {
        // 构建树状菜单返回
        List<SysMenu> menuList = sysMenuService.getAllMenu();
        return Result.ok(menuList);
    }

    /**
     * 增加菜单
     * @param sysMenu
     * @return
     */
    @PostMapping("save")
    public Result addMenu(@RequestBody SysMenu sysMenu) {

        boolean save = sysMenuService.save(sysMenu);
        if (save) {
            return Result.ok();
        }
        return Result.fail("增加失败");
    }

    /**
     * 修改菜单
     * @param sysMenu
     * @return
     */
    @PutMapping("update")
    public Result updateMenu(@RequestBody SysMenu sysMenu) {
        boolean update = sysMenuService.updateById(sysMenu);
        if (update) {
            return Result.ok();
        }
        return Result.fail("修改失败");
    }


    /**
     * 删除菜单
     */
    @DeleteMapping("remove/{id}")
    public Result removeMenu(@PathVariable Long id) throws ACGException {
        boolean remove = sysMenuService.deleteById(id);

        if (remove) {
            return Result.ok();
        }
        return Result.fail("删除失败");
    }

    //  @ApiOperation(value = "根据角色获取菜单")
    @GetMapping("toAssign/{roleId}")
    public Result toAssign(@PathVariable Long roleId) {
        List<SysMenu> list = sysMenuService.findSysMenuByRoleId(roleId);
        return Result.ok(list);
    }

    //  @ApiOperation(value = "给角色分配权限")
    @PostMapping("/doAssign")
    public Result doAssign(@RequestBody AssignMenuVo assignMenuVo) {
        sysMenuService.doAssign(assignMenuVo);
        return Result.ok();
    }

}

